import axios from 'axios';
import store from '../store';

// 创建axios实例
const service = axios.create({
    // baseURL: process.env.BASE_API, // api的base_url
    timeout: 50000, // 请求超时时间
});

// request拦截器
service.interceptors.request.use(config => {
    if(!config.baseURL || config.baseURL != "https://interface.91118.com"){
        if(store.getters.token){
            const TokenKey = store.getters.token
            config.headers.Authorization = TokenKey;
            if(!TokenKey){
                location.reload(); // 为了重新实例化vue-router对象 避免bug
            }
        }
    }
    return config;
}, error => {
    Promise.reject(error);
})

// respone拦截器
service.interceptors.response.use(
    response => {
        if(response.status === 200 || response.status === 304){
            return response.data;
        }else{
            console.log('网络不太给力哦，检查一下您的网络再试吧！')
        }
    },
    error => {
        console.log('err' + error); // for debug
        console.log('网络不太给力哦，检查一下您的网络再试吧！')
        return Promise.reject(error);
    }
);

export default service;


